package com.zhangc.blog.admin.web.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangc.blog.admin.entity.BlogLinkEntity;
import com.zhangc.blog.admin.service.BlogLinkServiceImpl;
import com.zhangc.blog.admin.web.dto.AjaxPutPage;
import com.zhangc.blog.admin.web.dto.AjaxResultPage;
import com.zhangc.blog.base.ResultBO;
import com.zhangc.blog.base.constants.LinkConstants;

/**
 * @qq交流群 796794009
 * @qq 1320291471
 * @Description: 友链Controller
 * @create 2020/12/27
 */
@Controller
@RequestMapping("/admin")
public class LinkController {

    @Autowired
    private BlogLinkServiceImpl blogLinkService;

    @GetMapping("/v1/linkType")
    public String gotoLink() {
        return "adminLayui/link-list";
    }

    @ResponseBody
    @GetMapping("/v1/linkType/list")
    public ResultBO<List<BlogLinkEntity>> linkTypeList() {
        List<BlogLinkEntity> links = new ArrayList<>();
        links.add(new BlogLinkEntity().setLinkType(LinkConstants.LINK_TYPE_FRIENDSHIP.getLinkTypeId())
                .setLinkName(LinkConstants.LINK_TYPE_FRIENDSHIP.getLinkTypeName()));
        links.add(new BlogLinkEntity().setLinkType(LinkConstants.LINK_TYPE_RECOMMEND.getLinkTypeId())
                .setLinkName(LinkConstants.LINK_TYPE_RECOMMEND.getLinkTypeName()));
        links.add(new BlogLinkEntity().setLinkType(LinkConstants.LINK_TYPE_PRIVATE.getLinkTypeId())
                .setLinkName(LinkConstants.LINK_TYPE_PRIVATE.getLinkTypeName()));
        return ResultBO.success();
    }

    @ResponseBody
    @GetMapping("/v1/link/paging")
    public AjaxResultPage<BlogLinkEntity> getLinkList(AjaxPutPage<BlogLinkEntity> ajaxPutPage, BlogLinkEntity condition) {
        QueryWrapper<BlogLinkEntity> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda()
                .orderByAsc(BlogLinkEntity::getLinkRank);
        Page<BlogLinkEntity> page = ajaxPutPage.putPageToPage();
        blogLinkService.page(page, queryWrapper);
        AjaxResultPage<BlogLinkEntity> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    @ResponseBody
    @PostMapping("/v1/link/isDel")
    public ResultBO<String> updateLinkStatus(BlogLinkEntity blogLink) {
        boolean flag = blogLinkService.updateById(blogLink);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }

    @ResponseBody
    @PostMapping("/v1/link/clear")
    public ResultBO<String> clearLink(Integer linkId) {
        boolean flag = blogLinkService.removeById(linkId);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }

    @GetMapping("/v1/link/edit")
    public String editLink(Integer linkId, Model model) {
        if (linkId != null) {
            BlogLinkEntity blogLink = blogLinkService.getById(linkId);
            model.addAttribute("blogLink", blogLink);
        }
        return "adminLayui/link-edit";
    }

    @ResponseBody
    @PostMapping("/v1/link/edit")
    public ResultBO<String> updateAndSaveLink(BlogLinkEntity blogLink) {
        blogLink.setCreateTime(new Date());
        boolean flag;
        if (blogLink.getLinkId() != null) {
            flag = blogLinkService.updateById(blogLink);
        } else {
            flag = blogLinkService.save(blogLink);
        }
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }
}
